home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6272 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  52 lines

  1. Path: newsbf02.news.aol.com!not-for-mail
  2. From: smalherbe@aol.com (SMalherbe)
  3. Newsgroups: comp.lang.c++
  4. Subject: Overloading operator->
  5. Date: 12 Feb 1996 02:16:29 -0500
  6. Organization: America Online, Inc. (1-800-827-6364)
  7. Sender: root@newsbf02.news.aol.com
  8. Message-ID: <4fmpgd$rko@newsbf02.news.aol.com>
  9. Reply-To: smalherbe@aol.com (SMalherbe)
  10.  
  11. I am attempting to implement a standard "indirection via a proxy"
  12. design pattern. I do this using the textbook overloading operator->
  13. approach. This works if the proxy (B in my example) is declared to
  14. be either automatic or static, but not on the heap. This is a
  15. significant limitation. The problem appears to be that I cannot
  16. do a "real" dereference and then use my overloaded operator without
  17. doing it explicitly (ie. "(*B)->"). This seems pretty ugly. Is there
  18. a way around this or am I missing something?
  19.  
  20. class A {
  21.    public:
  22.       A () : x (0)  {}
  23.       int f() const {return x;}
  24.    private:
  25.       int x;
  26. };
  27.                                 
  28. class B {
  29.    public:
  30.       B () {aPtr = new A;}
  31.       A *operator->() {return aPtr;}
  32.    private:
  33.       A *aPtr;
  34. };
  35.  
  36. main ()
  37. {
  38.    B  myB;
  39.    B *myBPtr = new B;
  40.  
  41.    myB->f();            // <--- This is OK
  42.    myBPtr->f();         // <--- This produces the following error:
  43.  
  44.    //   proxy.cpp(23:12) : error EDC3079: "f" is not a member of "B".
  45.  
  46.    (*myBPtr)->f();      // <--- Is also OK
  47. }
  48.  
  49. Thanks in advance for any advice,
  50.  
  51. Stephen Malherbe
  52.